<template>
  <div class="xjh-table">
    <div class="header">
      <div class="title">{{ headTitle }}</div>
      <div class="headerHandler">
        <slot name="headerHandler"></slot>
      </div>
    </div>
    <el-table
      :data="dataList"
      @selection-change="handleSelectionChange"
      style="width: 100%"
      v-bind="childrenProps"
    >
      <el-table-column v-if="showIndexColumn" type="selection" width="55" />
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
      ></el-table-column>
      <template v-for="propItem in propInfo" :key="propItem.prop">
        <el-table-column show-overflow-tooltip v-bind="propItem" align="center">
          <template #default="scope">
            <!-- 这里的断言真的是没有办法，为什么传值会发生这样的问题 -->
            <slot :name="(propItem as any).slotName" :row="scope.row">
              {{ scope.row[(propItem as any).prop] }}
            </slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <!-- v-model:currentPage="page.pageNum" -->
        <el-pagination
          :current-page="page.pageNum + 1"
          :page-sizes="[10, 20, 30]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="dataCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </slot>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
  dataList: {
    type: Array,
    required: true
  },
  dataCount: {
    type: Number,
    required: true
  },
  propInfo: {
    type: Array,
    required: true
  },
  showIndexColumn: Boolean,
  showSelection: Boolean,
  headTitle: String,
  showFooter: {
    type: Boolean,
    default: true
  },
  page: {
    type: Object,
    default: () => ({ pageSize: 10, pageNum: 0 })
  },
  childrenProps: {
    type: Object,
    default: () => ({})
  }
});

const emit = defineEmits(['tableSelectionChange', 'update:page']);

const handleSelectionChange = (val: any[]) => {
  // console.log(val);
  emit('tableSelectionChange', val);
};

const handleSizeChange = (pageSize: number) => {
  emit('update:page', { ...props.page, pageSize });
};
const handleCurrentChange = (pageNum: number) => {
  emit('update:page', { ...props.page, pageNum: pageNum - 1 });
};
</script>

<style lang="less" scoped>
.xjh-table {
  color: red;
  .header {
    display: flex;
    padding: 10px 30px;
    justify-content: space-between;
    align-items: center;
    .title {
      font-size: 20px;
    }
  }
  .footer {
    margin-top: 30px;
    width: 100%;
    text-align: right;
  }
}
</style>
